package com.example.demo.common.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import java.lang.reflect.Type;
import java.util.*;
import java.util.function.BiConsumer;

public class Utils {
    public static Boolean isNil(String str) {
        if (str == null) {
            return true;
        } else if (str.equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public static Boolean isNotNil(String str) {
        return !isNil(str);
    }

    public static Boolean isNotNil(ArrayList str) {
        return !isNil(str);
    }

    public static Boolean isNotNil(HashMap str) {
        return !isNil(str);
    }

    public static String int2str(int number) {
        return Convert.toStr(number);
    }

    public static String int2str(Integer number) {
        return Convert.toStr(number);
    }

    public static String toStr(Object obj) {
        return Convert.toStr(obj);
    }
    public static Integer toInt(String str) {
        return Convert.toInt(str);
    }


    public static List<?> array2list(long[] nb) {
        return Convert.toList(nb);
    }

    public static List<?> array2list(String[] nb) {
        return Convert.toList(nb);
    }

    public static List<?> array2list(Object[] nb) {
        return Convert.toList(nb);
    }

    public static String dateFormatYYYYMMddHHmmss(Date date) {
        return DateUtil.format(date, "YYYY-MM-dd HH:mm:ss");
    }
    public static String dateFormatYYYYMMdd(Date date) {
        return DateUtil.format(date, "YYYY-MM-dd");
    }

    public static String strFormat(String str, Object... params) {
        return StrUtil.format(str, params);
    }

    public static String createStr(String str, Object... params) {
        return StrUtil.format(str, params);
    }

    public static StrBuilder createStrBuilder(String str) {
        StrBuilder strBuilder = StrBuilder.create();
        strBuilder.append(str);
        return strBuilder;
    }
    public static StrBuilder createStrBuilder() {
        StrBuilder strBuilder = StrBuilder.create();
        return strBuilder;
    }


    public static Boolean isNil(Integer inter) {
        if (inter == null) {
            return true;
        } else if (inter == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static Boolean isNil(Object obj) {
        if (obj == null) {
            return true;
        } else {
            return false;
        }
    }

    public static Boolean isNil(ArrayList obj) {
        if (obj == null) {
            return true;
        } else if (obj.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static String uuidSnow() {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        long id = snowflake.nextId();
        return Convert.toStr(id);
    }

    //分布式 uuid
    public static String uuidDistributed() {
        String id2 = IdUtil.objectId();
        return Convert.toStr(id2);
    }

    //生成的UUID是带-的字符串，类似于：a5c8a5e8-df2b-4706-bea4-08d0939410e3
    public static String uuid() {
        //生成的UUID是带-的字符串，类似于：a5c8a5e8-df2b-4706-bea4-08d0939410e3
        String uuid = IdUtil.randomUUID();
        return uuid;
    }

    public static String tojsonStr(Object o) {
        return JSONUtil.toJsonPrettyStr(o);
    }
    public static String tojsonStrPlain(Object o) {
        return JSONUtil.toJsonStr(o);
    }

    public static <T> T jsonStr2Object(String str, Class<T> beanClass) {
        return JSONUtil.toBean(str, beanClass);
    }

    public static <T> T object2bean(Object temp, Type type) {
        return Convert.convert(type, temp);
    }

    //读取文件
    public static String readFile(String absolutePath) {
        FileReader fileReader = new FileReader(absolutePath);
        fileReader.setCharset(CharsetUtil.CHARSET_GBK);
        String result = fileReader.readString();
        return result;
    }

    //map


/*
* @example
*      HashMap<String,Object> hashmap1=new HashMap<>();
*      hashmap1.put("我是key","我是value");
*      myObjectMap(params, (key, value) -> {
                //dosomething
            });
* */
    public static <T>  HashMap<String, Object> myObjectMap(HashMap<String, Object> map, BiConsumer<String, ? super T> consumer) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String newKey = entry.getKey();
            consumer.accept(newKey,(T)entry.getValue());
        }
        return map;
    }

    public static Boolean isImage(String fileName) {
        String extension = "";
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            extension = fileName.substring(i + 1).toLowerCase();
        }
        if ("jpg".equals(extension)) {
            return true;
        }
        if ("png".equals(extension)) {
            return true;
        }
        if ("webp".equals(extension)) {
            return true;
        }
       /* if ("gif".equals(extension)) {
            return true;
        }*/
        if ("svg".equals(extension)) {
            return true;
        }
        return false;
    }

    public static Boolean isVideo(String fileName) {
        String extension = "";
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            extension = fileName.substring(i + 1).toLowerCase();
        }
        if ("mp4".equals(extension)) {
            return true;
        }
        return false;
    }
    public static String getFileSuffix(String fileName) {
        String extension = "";
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            extension = fileName.substring(i + 1).toLowerCase();
        }
        return extension;
    }

}
